import {getUsername} from "./utils.js"
import {getRole} from "./utils.js"
import {uploadFile} from "./utils.js"

let newImg = ""
const file = $(".file")

file.change(function () {
    uploadFile(file).then((url) => {
        newImg = url
    })
})

$(".submit").click(() => {
    const username = getUsername()
    const role = getRole()

    $.ajax({
        url: "http://www.woniu-news.com:8082/addNews.php",
        type: "POST",
        xhrFields: {
            withCredentials: true
        },
        data: {
            username,
            role,
            newImg,
            title: $("#title").val(),
            context: $("#content").val(),
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                location.href = "index.html"
            } else {
                alert(resp.message)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
})